package face.pay.common.enterprise.service.impl;

import com.aliyun.oss.model.PutObjectResult;
import com.aliyun.oss.sdk.OSSUtils;
import face.pay.common.enterprise.service.IBaseMchInfoService;
import face.pay.dao.BaseMchInfoDao;
import face.pay.dao.FileInfoDao;
import face.pay.dao.OemConfigInfoDao;
import face.pay.entity.BaseMchInfo;
import face.pay.entity.FileInfo;
import face.pay.entity.OemConfigInfo;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Auther: cjj
 * @Date: 2019/9/18
 * @Description:
 */
@Service
@Slf4j
public class IBaseMchInfoServiceImpl implements IBaseMchInfoService {

    @Autowired
    private BaseMchInfoDao baseMchInfoDao;


    @Autowired
    private OemConfigInfoDao oemConfigInfoDao;

    @Autowired
    private FileInfoDao fileInfoDao;

    /**
     * 根据用户id去查商户基本信息
     *
     * @param
     * @return
     */
    @Override
    public BaseMchInfo queryBaseMachInfoByUserId(UserInfo loginUser) {
        BaseMchInfo record = new BaseMchInfo();
        record.setUserId(loginUser.getId());
        BaseMchInfo baseMchInfo = this.baseMchInfoDao.queryBaseInfo(record);
        if (baseMchInfo == null) {
            //没查到说明是第一次登陆还没有基本资料
            BaseMchInfo info = new BaseMchInfo();
            info.setUserId(loginUser.getId());
            info.setOemId(loginUser.getOemId());
            info.setCreatedBy(loginUser.getId());
            this.baseMchInfoDao.insert(info);
            return info;
        } else {
            return baseMchInfo;
        }
    }

    /**
     * 文件上传
     *
     * @param fileBytes
     * @param originalFilename
     * @param loginUser
     * @return
     */
    @Override
    public ResultMessage fielUpload(byte[] fileBytes, String originalFilename, UserInfo loginUser) {
        //获取oem商的oss配置信息
        OemConfigInfo oemConfig = this.oemConfigInfoDao.getByOemId(loginUser.getOemId());
        if (oemConfig == null) {
            return ResultMessage.createFail();
        }
        //生成fielInfo和fullname
        FileInfo fileInfo = new FileInfo(oemConfig.getOemId(), loginUser.getId(), originalFilename, "");
        String fullName = fileInfo.createFullName(oemConfig.getOemId());
        //推送到oss
        PutObjectResult putObjectResult = OSSUtils.putOSS(oemConfig.getEndPoint(), oemConfig.getAccessKeyId(), oemConfig.getAccessKeySecret(), oemConfig.getBucket(), fileInfo.getFullName(), fileBytes, false);
        if (null == putObjectResult) {
            return ResultMessage.createFail();
        }
        fileInfo.setCreatedBy(loginUser.getId());
        fileInfo.setUpdatedBy(loginUser.getId());
        this.fileInfoDao.insert(fileInfo);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", fileInfo.getId());
        return ResultMessage.createSuccess(map);
    }

    /**
     * 获取文件
     *
     * @param fielId
     * @param loginUser
     * @return
     */
    @Override
    public byte[] getMedia(String fielId, UserInfo loginUser) {
        Integer oemId = loginUser.getOemId();

        if (StringUtils.isBlank(fielId)) {
            return new byte[0];
        }
        FileInfo fileInfo = this.fileInfoDao.getById(new Long(fielId));
        //获取oem的oss信息和文件fullname
        OemConfigInfo oemConfigInfo = oemConfigInfoDao.getByOemId(loginUser.getOemId());

        byte[] bytes = new byte[]{};
        try {
            bytes = OSSUtils.getOSSBytes(oemConfigInfo.getEndPoint(), oemConfigInfo.getAccessKeyId(), oemConfigInfo.getAccessKeySecret(), oemConfigInfo.getBucket(), fileInfo.getFullName());
        } catch (Exception e) {
            log.error("下载图片异常", e);
            return new byte[0];
        }
        return bytes;
    }

    /**
     * 编辑商家资料
     *
     * @param baseMchInfo
     * @param loginUser
     * @return
     */
    @Override
    public ResultMessage edit(BaseMchInfo baseMchInfo, UserInfo loginUser) {
        if (baseMchInfo.getId() == null) {
            //商家第一次填写资料
            baseMchInfo.setOemId(loginUser.getOemId());
            baseMchInfo.setUserId(loginUser.getId());
            baseMchInfo.setCreatedBy(loginUser.getId());
            baseMchInfo.setUpdatedBy(loginUser.getId());
            this.baseMchInfoDao.insert(baseMchInfo);
            return ResultMessage.createSuccess();
        } else {
            //商家是修改资料
            baseMchInfo.setUpdatedBy(loginUser.getId());
            this.baseMchInfoDao.update(baseMchInfo);
            return ResultMessage.createSuccess();
        }
    }
}
